import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;


import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;


public class P02_Panel extends JPanel  implements MouseListener, MouseMotionListener, ActionListener {
	
	private PanelListener listener;
	
	//1920
	private int width=0;
	//1080
	private int height=0;
	
	//menu control
	private Image[] imageMenu = new Image[9];
	private boolean[] menu_presseed = new boolean[4];
	private boolean help_pressed = false;
	
	private Image[] imageList = new Image[47];
	
	public int man=0;
	
	private boolean[] buttons_pressed = new boolean[2];
	private int buttons_position=-1;
	
	private int position=0;
	
	private boolean moving=false;
	
	private Timer timer;
	private boolean long_pressed=false;
	
	public P02_Panel(int width, int height){
		
		this.width=width;
		this.height=height;
		
		//menu control
		imageMenu[0]=new ImageIcon(getClass().getResource("images/m/h0.png")).getImage();
		imageMenu[1]=new ImageIcon(getClass().getResource("images/m/h1.png")).getImage();
		imageMenu[2]=new ImageIcon(getClass().getResource("images/m/f0.png")).getImage();
		imageMenu[3]=new ImageIcon(getClass().getResource("images/m/f1.png")).getImage();
		imageMenu[4]=new ImageIcon(getClass().getResource("images/m/b0.png")).getImage();
		imageMenu[5]=new ImageIcon(getClass().getResource("images/m/b1.png")).getImage();
		imageMenu[6]=new ImageIcon(getClass().getResource("images/m/q0.png")).getImage();
		imageMenu[7]=new ImageIcon(getClass().getResource("images/m/q1.png")).getImage();
		imageMenu[8]=new ImageIcon(getClass().getResource("images/m/help/02.png")).getImage();
		
		menu_presseed[0]=false;
		menu_presseed[1]=false;
		menu_presseed[2]=false;
		menu_presseed[3]=false;
		
		
		imageList[0]=new ImageIcon(getClass().getResource("images/2/background.png")).getImage();
		imageList[1]=new ImageIcon(getClass().getResource("images/2/arrow.png")).getImage();
		imageList[2]=new ImageIcon(getClass().getResource("images/2/man.png")).getImage();
		imageList[3]=new ImageIcon(getClass().getResource("images/2/woman.png")).getImage();
		imageList[4]=new ImageIcon(getClass().getResource("images/2/ruler.png")).getImage();
		imageList[5]=new ImageIcon(getClass().getResource("images/2/down00.png")).getImage();
		imageList[6]=new ImageIcon(getClass().getResource("images/2/down01.png")).getImage();
		imageList[7]=new ImageIcon(getClass().getResource("images/2/up00.png")).getImage();
		imageList[8]=new ImageIcon(getClass().getResource("images/2/up01.png")).getImage();
		
		imageList[9]=new ImageIcon(getClass().getResource("images/2/m00.png")).getImage();
		imageList[10]=new ImageIcon(getClass().getResource("images/2/m01.png")).getImage();
		imageList[11]=new ImageIcon(getClass().getResource("images/2/m02.png")).getImage();
		imageList[12]=new ImageIcon(getClass().getResource("images/2/m03.png")).getImage();
		imageList[13]=new ImageIcon(getClass().getResource("images/2/m04.png")).getImage();
		imageList[14]=new ImageIcon(getClass().getResource("images/2/m05.png")).getImage();
		imageList[15]=new ImageIcon(getClass().getResource("images/2/m06.png")).getImage();
		imageList[16]=new ImageIcon(getClass().getResource("images/2/m07.png")).getImage();
		imageList[17]=new ImageIcon(getClass().getResource("images/2/m08.png")).getImage();
		imageList[18]=new ImageIcon(getClass().getResource("images/2/m09.png")).getImage();
		imageList[19]=new ImageIcon(getClass().getResource("images/2/m10.png")).getImage();
		imageList[20]=new ImageIcon(getClass().getResource("images/2/m11.png")).getImage();
		imageList[21]=new ImageIcon(getClass().getResource("images/2/m12.png")).getImage();
		imageList[22]=new ImageIcon(getClass().getResource("images/2/m13.png")).getImage();
		imageList[23]=new ImageIcon(getClass().getResource("images/2/m14.png")).getImage();

		imageList[24]=new ImageIcon(getClass().getResource("images/2/w00.png")).getImage();
		imageList[25]=new ImageIcon(getClass().getResource("images/2/w01.png")).getImage();
		imageList[26]=new ImageIcon(getClass().getResource("images/2/w02.png")).getImage();
		imageList[27]=new ImageIcon(getClass().getResource("images/2/w03.png")).getImage();
		imageList[28]=new ImageIcon(getClass().getResource("images/2/w04.png")).getImage();
		imageList[29]=new ImageIcon(getClass().getResource("images/2/w05.png")).getImage();
		imageList[30]=new ImageIcon(getClass().getResource("images/2/w06.png")).getImage();
		imageList[31]=new ImageIcon(getClass().getResource("images/2/w07.png")).getImage();
		imageList[32]=new ImageIcon(getClass().getResource("images/2/w08.png")).getImage();
		imageList[33]=new ImageIcon(getClass().getResource("images/2/w09.png")).getImage();
		imageList[34]=new ImageIcon(getClass().getResource("images/2/w10.png")).getImage();
		imageList[35]=new ImageIcon(getClass().getResource("images/2/w11.png")).getImage();
		imageList[36]=new ImageIcon(getClass().getResource("images/2/w12.png")).getImage();
		imageList[37]=new ImageIcon(getClass().getResource("images/2/w13.png")).getImage();
		imageList[38]=new ImageIcon(getClass().getResource("images/2/w14.png")).getImage();
		
		imageList[39]=new ImageIcon(getClass().getResource("images/2/m/00.png")).getImage();
		imageList[40]=new ImageIcon(getClass().getResource("images/2/m/01.png")).getImage();
		imageList[41]=new ImageIcon(getClass().getResource("images/2/m/02.png")).getImage();
		imageList[42]=new ImageIcon(getClass().getResource("images/2/m/03.png")).getImage();
		
		imageList[43]=new ImageIcon(getClass().getResource("images/2/w/00.png")).getImage();
		imageList[44]=new ImageIcon(getClass().getResource("images/2/w/01.png")).getImage();
		imageList[45]=new ImageIcon(getClass().getResource("images/2/w/02.png")).getImage();
		imageList[46]=new ImageIcon(getClass().getResource("images/2/w/03.png")).getImage();
		
		addMouseListener(this);
		addMouseMotionListener(this);
	  	Dimension size = new Dimension(width, height);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		setSize(size);
		setLayout(null);
		
		buttons_pressed[0]=false;
		buttons_pressed[1]=false;
				
	}
	  

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		//draw background
		g.drawImage(imageList[0], 0, 0, null);
		
		//draw menu control

		//home
		if(menu_presseed[0]){
			g.drawImage(imageMenu[1], 1800, 20, null);
		}else{
			g.drawImage(imageMenu[0], 1800, 20, null);
		}
		
		
		//back
		
		if(menu_presseed[1]){
			g.drawImage(imageMenu[5], 20, 950, null);
		}else{
			g.drawImage(imageMenu[4], 20, 950, null);
		}
		
		
		//forward
		if(menu_presseed[2]){
			g.drawImage(imageMenu[3], 1830, 950, null);
		}else{
			g.drawImage(imageMenu[2], 1830, 950, null);
		}
		
		//help
		if(menu_presseed[3]){
			g.drawImage(imageMenu[7], 1732, 20, null);
		}else{
			g.drawImage(imageMenu[6], 1732, 20, null);
		}
		
		//help content
		if(help_pressed){
			g.drawImage(imageMenu[8], 1365, 79, null);
		}
				
		//ruler
		g.drawImage(imageList[4], 911, 211, null);
		
		//arrow
		g.drawImage(imageList[1], 966, 429, null);
		
		//human
		//position : 0-14
		if(man==0){
			//man
			g.drawImage(imageList[2], 690, 467, null);
			
			//0-1
			//2-5
			//6-8
			//9-
			//comment
			//244,274
			if(position<2){
				g.drawImage(imageList[39], 244, 274, null);
			}else if(position<6){
				g.drawImage(imageList[40], 244, 274, null);
			}else if(position<9){
				g.drawImage(imageList[41], 244, 274, null);
			}else{
				g.drawImage(imageList[42], 244, 274, null);
			}
			
			
		}else{
			//woman
			
			//0-2
			//3-5
			//6-10
			//11-
			if(position<3){
				g.drawImage(imageList[43], 690, 467-46-46-47, null);
			}else if(position<6){
				g.drawImage(imageList[44], 690, 467-46-46, null);
			}else if(position<11){
				g.drawImage(imageList[45], 690, 467-46, null);
			}else{
				g.drawImage(imageList[46], 690, 467, null);
			}		
			
			//g.drawImage(imageList[2], 690, 467, null);
		}
		//g.drawImage(imageList[2+man], 690, 467, null);
	
		//buttons
		
		//up : 99 * 55
		if(!buttons_pressed[0]){
			g.drawImage(imageList[7], 1107, 357, null);
		}else{
			g.drawImage(imageList[8], 1107, 357, null);
		}
		
		
		//down
		if(!buttons_pressed[1]){
			g.drawImage(imageList[5], 1107, 530, null);
		}else{
			g.drawImage(imageList[6], 1107, 530, null);
		}
		
		//arrow value
		//m : 09-23
		//w : 24-38
		
		//m : 2
		//w : 9-13
		int position_=9+(man*15)+position;
		int y_=443;
		if(man==0){
			if(position==2)
				y_+=2;
		}else{
			if(position>=9 && position<=13)
				y_+=2;
		}
		
		
		g.drawImage(imageList[position_], 1026, y_, null);
		
	}
	
	public void mousePressed(MouseEvent e) {
		
		//home
		if(1800<=e.getX() && e.getX()<=1800+63 
				&& 40<=e.getY() && e.getY()<=40+57){
			menu_presseed[0]=true;
		}
		
		//back
		
		if(20<=e.getX() && e.getX()<=20+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			menu_presseed[1]=true;
		}
		
		
		//forward
		if(1830<=e.getX() && e.getX()<=1830+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			menu_presseed[2]=true;
		}
		
		//help
		if(1732<=e.getX() && e.getX()<=1732+60 
				&& 20<=e.getY() && e.getY()<=20+60){
			menu_presseed[3]=true;
		}
		
		//up : 99 * 55
		if(1107<=e.getX() && e.getX()<=1107+99 
				&& 357<=e.getY() && e.getY()<=357+55){
			buttons_pressed[0]=true;
			buttons_position=0;
		}
		
		if(1107<=e.getX() && e.getX()<=1107+99 
				&& 530<=e.getY() && e.getY()<=530+55){
			buttons_pressed[1]=true;
			buttons_position=1;
		}
		
		timer = new Timer(1200, this);
		long_pressed=false;
		timer.start();
		
		
		this.repaint();
    }

    public void mouseReleased(MouseEvent e) {
    	
    	timer.stop();
    	
    	if(menu_presseed[0] && 1800<=e.getX() && e.getX()<=1800+63 
				&& 40<=e.getY() && e.getY()<=40+57){
			//home
    		if(!moving){
    			listener.onHome();
    			moving=true;
    		}
		}else if(menu_presseed[1] && 20<=e.getX() && e.getX()<=20+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			//back
			if(!moving){
				listener.onBack();
				moving=true;
			}
		}else if(menu_presseed[2] && 1830<=e.getX() && e.getX()<=1830+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			//forward
			if(!moving){
				listener.onChangeValue(2, position);
				listener.onForward();
				moving=true;
			}
		}else if(menu_presseed[3] && 1732<=e.getX() && e.getX()<=1732+60 
				&& 20<=e.getY() && e.getY()<=20+60){
			help_pressed=!help_pressed;
		}
    	
    	menu_presseed[0]=false;
    	menu_presseed[1]=false;
    	menu_presseed[2]=false;
		menu_presseed[3]=false;
    	
    	//up : 99 * 55
		if(buttons_position==0 && 1107<=e.getX() && e.getX()<=1107+99 
				&& 357<=e.getY() && e.getY()<=357+55){
			spinArrow(true);
		}
		
		if(buttons_position==1 && 1107<=e.getX() && e.getX()<=1107+99 
				&& 530<=e.getY() && e.getY()<=530+55){
			spinArrow(false);
		}
    	
    	buttons_pressed[0]=false;
		buttons_pressed[1]=false;
		buttons_position=-1;
		
    	this.repaint();
    }
    
    public void mouseEntered(MouseEvent e) {
       
    }

    public void mouseExited(MouseEvent e) {
       
    }

    public void mouseClicked(MouseEvent e) {
    	
    }
    
    public void mouseMoved(MouseEvent e) {
        
    	
    }
    
    public void mouseDragged(MouseEvent e) {
        
    	
    }
    
    public void setPanelListener(PanelListener listener){
    	this.listener=listener;
    }
    
    private void spinArrow(boolean up){
    	if(up){
    		--position;
    		if(position<0){
    			position=14;
    		}
    	}else{
    		++position;
    		if(position>14){
    			position=0;
    		}
    	}
    }
    
    public void actionPerformed(ActionEvent e) {
    	
    	if(!long_pressed){
    		timer.stop();
    		timer = new Timer(150, this);
    		timer.start();
    		long_pressed=true;
    	}else{
    		int x = (int)MouseInfo.getPointerInfo().getLocation().getX();
    		int y = (int)MouseInfo.getPointerInfo().getLocation().getY();
    		
    		if(1107<=x && x<=1107+99 
    				&& 357<=y && y<=357+55){
    			spinArrow(true);
    		}
    		
    		if(1107<=x && x<=1107+99 
    				&& 530<=y && y<=530+55){
    			spinArrow(false);
    		}
    		
    		this.repaint();
    	}
    }
}
